import pygame, sys, math, random
 
pygame.init()
   
from Player import Player
from Animal import Animal
from Bullet import Bullet

clock = pygame.time.Clock()

width = 1024
height = 768
size = width, height

screen = pygame.display.set_mode(size)
#background image is taken from http://commons.wikimedia.org/wiki/File:Desert_de_Retz_Grass_02.jpg


animals = []

bullets = []

#startButton = Button("RSC/buttons/BALL.png", [width/2, height/3], [400,200])

player = Player("Rsc/hunter.png", [5,5], (width/2, height/2))

start = False
while True:
    bgImage = pygame.image.load("Start.png")
    bgRect = bgImage.get_rect()
    
    while not start:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    start = True
         
        screen.blit(bgImage, bgRect)
        pygame.display.flip()
        clock.tick(60)
    
     
    level = 0
    bgImage = pygame.image.load("Rsc/Prairie_grass.png")
    
    while start and player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player.direction("right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player.direction("left")
                if event.key == pygame.K_w or event.key == pygame.K_UP: 
                    player.direction("up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player.direction("down")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player.direction("stop right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player.direction("stop left")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    player.direction("stop up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player.direction("stop down")
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    bullets += [Bullet(player.rect.center, player.angle)]
        
        for animal in animals:
            animal.update()
                           
        for animal in animals:
            animal.collideWall(width, height)
            
            
        if len(animals) > 1:
            for first in range(len(animals)-1):
                for second in range(first+1,len(animals)):
                    animals[first].collideAnimal(animals[second])
        
        for animal in animals:
            player.collideAnimal(animal)
            
        for animal in animals:
            if not animal.living:
                animals.remove(animal)
                
        print len(bullets)
        for bullet in bullets:
            bullet.update()
            bullet.collideWall(width,height)
            for animal in animals:
                bullet.collideAnimal(animal)
                #print "Hi"
            if not bullet.living:
                bullets.remove(bullet)
                
        
        if len(animals) == 0:
            level += 1
            for i in range(level):
                print i
                bsize = random.randint(25, 100)
                ranimal = random.randint(0,2)
                collideing = True
                while collideing: 
                    if ranimal == 0:
                        animal = Animal("Deer.png",
                                         [random.randint(-4,4), random.randint(-4,4)], 
                                         [bsize, bsize], 
                                         [random.randint(75, width-75), random.randint(75, height-75)])
                    elif ranimal == 1:
                        animal = Animal("rsc/Bear/bear1.png",
                                         [random.randint(-3,3), random.randint(-3,3)], 
                                         [bsize, bsize], 
                                         [random.randint(75, width-75), random.randint(75, height-75)])
                    elif ranimal == 2:
                        animal = Animal("rsc/Duck/Duck.png",
                                     [random.randint(-5,5), random.randint(-5,5)], 
                                     [bsize, bsize], 
                                     [random.randint(75, width-75), random.randint(75, height-75)])
                 
                    if not animal.collidePlayer(player):
                        collideing = False
                animals += [animal]
        
        
        player.update()
        player.collideWall(width, height)
        screen.blit(bgImage, bgRect)
        for animal in animals:
            screen.blit(animal.image, animal.rect)
        for bullet in bullets:
            screen.blit(bullet.image, bullet.rect)
        screen.blit(player.image, player.rect)
        pygame.display.flip()
        clock.tick(60)
        
    bgImage = pygame.image.load("Untitled.png")
    while start and not player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    start = False
                    bullets = []
                    animals = []
                    level = 0
                    player = Player("Rsc/hunter.png", [5,5], (width/2, height/2))

         
        screen.blit(bgImage, bgRect)
        pygame.display.flip()
        clock.tick(60)